from Lib.ClassSelenium import ClassSelenium
from selenium.webdriver.common.keys import Keys
import time
from Lib.Log import logger
from Lib.Common import setFail
import Lib.Common as Common

def login(username, password):
    # 登入
    url = 'http://10.20.19.161/Artemis/login/login.html'
    global driver
    driver = ClassSelenium(url, 'chrome')
    element = driver.getElementByAttribute('xpath://input[@name="username"]')
    element.clear()
    element.send_keys(username)
    element = driver.getElementByAttribute('id:user_password')
    element.clear()
    element.send_keys(password)
    element = driver.getElementByAttribute('id:login_submit')
    element.click()
    logger.info("打开测试网址：" + url)
    logger.info(driver.driver.page_source)
    return driver

def isElementPresent(element):
    # 元素是否存在
    temp = element.split(':')
    logger.info('正在加载，请稍后...')
    elementObj = driver.findElement(temp[1], flag=False)
    if elementObj:
        logger.info('页面元素已经找到!')
    else:
        logger.error('页面元素没有找到！！！')
        driver.close()
        exit()

def matchMethod(clickElement, controlElement,msg):
    # 下拉框控件匹配方法
    element = driver.getElementByAttribute(clickElement)
    element.click()
    elementList = driver.getElementByAttribute(controlElement, getAll=True)
    for element in elementList:
        if element.getText() == msg:
            time.sleep(0.5)
            element.click()
            logger.info('选中的内容为:%s' %msg)
            break

def selectMethod(clickElement,inputElement,msg,selectElement):
    #部门形式下拉多选查询框
    element = driver.getElementByAttribute(clickElement)
    element.click()
    element = driver.getElementByAttribute(inputElement)
    element.clear()
    element.send_keys(msg)
    logger.info('输入信息：%s' %msg)
    element.send_keys(Keys.ENTER)
    element = driver.getElementByAttribute(selectElement)
    element.click()
    logger.info('选中的内容为:%s' %msg)

def getMenu(menuName,elementObj):
    # 定位到具体菜单
    element = driver.getElementByAttribute('xpath://*[@id="dijit_form_TextBox_0"]')
    element.clear()
    element.send_keys(menuName)
    time.sleep(0.5)
    element.send_keys(Keys.ENTER)
    time.sleep(1)
    logger.info('测试开始：定位到【%s】菜单' %menuName)
    element = driver.getElementByAttribute(elementObj)
    time.sleep(0.5)
    element.click()
#点击操作
def buttonclick(elementObj,msg):
    # 单击按钮
    element = driver.getElementByAttribute(elementObj)
    if not element:
        driver.close()
    element.click()
    time.sleep(0.5)
    logger.info('点击%s'%str(msg))


def write(msg, elementObj):
    # 输入元素
    element = driver.getElementByAttribute(elementObj)
    element.clear()
    element.send_keys(msg)
    logger.info('输入信息为：%s' % msg)


def writeMethod(msg,elementObj):
    # 输入元素并敲击回车
    element = driver.getElementByAttribute(elementObj)
    element.clear()
    element.send_keys(str(msg))
    time.sleep(0.5)
    element.send_keys(Keys.ENTER)
    logger.info('输入信息为：%s' % msg)

def export(elementObj, exportElementObj):
    # 点击导出
    element = driver.getElementByAttribute(elementObj)
    element.click()
    logger.info('测试【导出】功能')
    logger.info('点击“导出”')
    element = driver.getElementByAttribute(exportElementObj)
    logger.info(element.getText())
    if not element:
        logger.info('下载框元素获取失败')
        setFail("导出失败！")
        driver.close()
        exit()
    elif element.getText() == str('正在下载请稍候') or element.getText() == str('正在导出请稍候'):
        logger.info('“导出”成功')
    time.sleep(15)

def check(rightMsg,elementObj ):
    # 设置断言
    checkMsg = driver.getElementByAttribute(elementObj)
    checkMsg = checkMsg.getText()
    logger.info('期望信息为：%s' % rightMsg)
    logger.info('断言信息为：%s' % checkMsg)
    if rightMsg == str(checkMsg):
        msg = "断言成功"
        logger.info('断言结果为：%s' % msg)
    else:
        msg = "断言失败！"
        driver.getScreen()
        setFail("断言失败！")
        logger.error('断言结果为：%s' % msg)
        driver.close()
        exit()

def checkequal(wantmsg,getmsg ):
    # 设置断言
    logger.info('期望信息为：%s' % wantmsg)
    logger.info('断言信息为：%s' % getmsg)
    if str(wantmsg) == str(getmsg):
        msg = "断言成功"
        logger.info('断言结果为：%s' % msg)
    else:
        msg = "断言失败！"
        driver.getScreen()
        setFail("断言失败！")
        logger.error('断言结果为：%s' % msg)
        driver.close()
        exit()

def checknotequal(rightMsg,wantMsg):
    # 判断传入的两个文本是否不相等
    logger.info('期望信息为：%s' % rightMsg)
    logger.info('断言信息为：%s' % wantMsg)
    if rightMsg != str(wantMsg):
        msg = "断言成功"
        logger.info('断言结果为：%s' % msg)
    else:
        msg = "断言失败！"
        driver.getScreen()
        setFail("断言失败！")
        logger.error('断言结果为：%s' % msg)
        driver.close()
        exit()

def checkreset(elementObj1,elementObj2):
    # 设置断言
    checkMsg1 = driver.getElementByAttribute(elementObj1)
    checkMsg1 = checkMsg1.getText()
    checkMsg2 = driver.getElementByAttribute(elementObj2)
    checkMsg2 = checkMsg2.getText()
    logger.info('期望信息为：%s' % checkMsg1)
    logger.info('断言信息为：%s' % checkMsg2)
    if checkMsg1 == str(checkMsg2):
        msg = "断言成功"
        logger.info('断言结果为：%s' % msg)
    else:
        msg = "断言失败！"
        driver.getScreen()
        setFail("断言失败！")
        logger.error('断言结果为：%s' % msg)
        driver.close()
        exit()

def close(menuName):
    # 关闭测试窗口
    driver.close()
    logger.info('关闭【%s】测试窗口 '% menuName)

def confirm():
    # 弹出框确定
    driver.getAlert()

def upload(clickElement, uploadElement, uploadFile, cofirmElement):
    #测试导入功能
    elementObj = driver.getElementByAttribute(clickElement)
    elementObj.click()
    logger.info('测试【导入】功能')
    logger.info('点击“导入”')
    elementObj= driver.findElement(uploadElement)
    elementObj.send_keys(uploadFile)
    time.sleep(1)
    logger.info('导入“附件”')
    elementObj = driver.getElementByAttribute(cofirmElement)
    elementObj.click()
    logger.info('点击“确定”')
    time.sleep(2)

def checkbox(clickElement):
    #返回复选框的选中状态，TRUE为选中，false为未选中
    elementObj = driver.findElement(clickElement)
    elementObj = elementObj.is_selected()
    return elementObj

def msgclear(elementObj):
    #清空输入框
    elementObj = driver.getElementByAttribute(elementObj)
    elementObj.clear()
    elementObj.send_keys(Keys.ENTER)
    logger.info('清除信息')

def gettext(elementObj):
    #获取文本
    checkMsg = driver.getElementByAttribute(elementObj)
    checkMsg = checkMsg.getText()
    return checkMsg

def gettagtext(elementObj,tag):
    #获取元素标签对应的文本
    checkMsg = driver.getElementByAttribute(elementObj)
    checkMsg = checkMsg.getAttribute(tag)
    return checkMsg

def gettablecolu(n,element,msg):
    '''
    n:所要校验的参数个数
    element:所要校验行的元素（列表形式）
    msg:所要校验行的文本（列表形式）
    '''
    msg1=msg
    for index1,i in enumerate(element):
        if index1==0:
            global dict
            dict={}
        dict=gettablerow(index1,i,msg1[index1],dict)
        if index1==n-1 and len(dict)==1:
            logger.info('查询到数据')
            for value in dict.values():
                print(value)
                value.click()
            logger.info('选中数据')
            time.sleep(10)
        elif index1==n-1 and len(dict)!=1:
            setFail("断言失败！")
            logger.error('无法校验到期望的唯一数据')
            driver.close()
            exit()

def gettablerow(index,element,msg,dict):
    #判断是否为第一列，如果为第一列，将所有行号传入列表
    if index==0:
        elementList = driver.getElementByAttribute(element, getAll=True)
        for index2, i in enumerate(elementList):
            dict[index2]=i
    #对某一列的所有行进行文本校验，如果文本不符合，将不符合的行号进行剔除
    elementList1 = driver.getElementByAttribute(element, getAll=True)
    for index1, element in enumerate(elementList1):
        #存在于列表内的数据是否相等于所要校验数据，不相等，将该行号移除
        if index1 in dict:
            # element.click()
            # time.sleep(0.5)
            checkMsg = element.getText()
            if msg != checkMsg:
                del dict[index1]
    return dict

def repeatclick(elementObjs,msgs):
    #重复点击
    list1=[]
    list2=[]
    for elementObj in elementObjs:
        list1.append(elementObj)
    for msg in msgs:
        list2.append(msg)
    for i in range(len(list1)):
        buttonclick(list1[i],list2[i])

def repeatwrite(msgs,elementObjs):
    #重复写入
    list1 = []
    list2 = []
    for elementObj in elementObjs:
        list2.append(elementObj)
    for msg in msgs:
        list1.append(msg)
    for i in range(len(list1)):
        writeMethod(list1[i], list2[i])

def repeatcheck(msgs,elementObjs):
    #重复校验
    list1 = []
    list2 = []
    for elementObj in elementObjs:
        list2.append(elementObj)
    for msg in msgs:
        list1.append(msg)
    for i in range(len(list1)):
        check(list1[i], list2[i])

def clicksub(elementObj,msg):
    #保存之后提交
    buttonclick(elementObj,msg)
    confirm()

def auditselect(msg1,msg2,passelement,nopasselement):
    #审核选择
    if msg1 == msg2:
        buttonclick(passelement, '审核通过按钮')
    else:
        buttonclick(nopasselement, '审核不通过按钮')


def audit_item_information_is_set_manually(msgID,msgName,msgflag):
    '''
    msgID:审核ID
    msgName:审核名称
    msgflag:启用标志
    '''
    # 是否开启审核
    # 打开审核开关菜单
    getMenu('审核项信息手工设置', 'xpath://a[@title="审核项信息手工设置"]')
    # 输入审核ID
    writeMethod(msgID, 'xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120339-1"]/div[2]/form/div/div[1]/div/input')
    # 输入审核名称
    writeMethod(msgName, 'xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120339-1"]/div[2]/form/div/div[2]/div/input')
    a = ['xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120339-1"]/div[1]/button[1]',
         'xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120339_sollid"]/div[2]/div[1]/div/table/tbody/tr[1]/td[2]/span',
         'xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120339-btn_Modify"]/span',
         'xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120339_sollid"]/div[2]/div[1]/div/table/tbody/tr[1]/td[4]/div[1]/div[1]/div[1]/a[1]/span[2]']
    b = ['审核查询按钮', '资金录入审核数据', '审核修改按钮', '启用标志下拉框']
    repeatclick(a, b)
    # 输入启用标志
    writeMethod(msgflag, 'xpath://*[@id="select2-drop"]/div/input')
    # 点击审核保存按钮
    buttonclick('xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120339-1"]/div[3]/div[6]/div[1]/div/div[2]/div[2]/button[3]/span', '审核保存按钮')
    # 校验查询到的数据
    a = [msgID, msgflag]
    b = ['xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120339_sollid"]/div[2]/div[1]/div/table/tbody/tr[1]/td[2]/span',
         'xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120339_sollid"]/div[2]/div[1]/div/table/tbody/tr[1]/td[4]/span']
    repeatcheck(a, b)
    # 关闭审核开关tebe页
    buttonclick('xpath://*[@id="content_tablist"]/div[4]/div/div[2]/span[3]', '页面关闭')
    time.sleep(2)

def fund_transfer_review(tMethodmsg,auditDataele,auditResultele,notemsg,flag=0,department=0,account=0):
    '''
    tMethodmsg:调拨方式
    auditDataele:审核数据
    auditResultele:审核结果
    notemsg:备注数据
    '''
    buttonclick('xpath://*[@id="content_tablist"]/div[4]/div/div[2]/span[3]', '页面关闭')
    getMenu('资金调拨审核', 'xpath://a[@title="资金调拨审核"]')
    # 筛选出审核的数据
    writeMethod(tMethodmsg, 'xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120344-searchForm"]/div/table/tbody/tr/td[2]/div/div[3]/input[1]')
    writeMethod('新增待审核', 'xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120344-searchForm"]/div/table/tbody/tr/td[4]/div/div[3]/input[1]')
    buttonclick('xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120344-btn_Search_label"]', '审核菜单查询按钮')
    if notemsg == '资金分配uitest':
        # 直接点击审核按钮出现提示信息
        clicksub('xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120344-btn_AuditPass_label"]', '审核通过按钮')
        clicksub('xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120344-btn_AuditNoPass_label"]', '审核不通过按钮')
    else:
        pass
    # 选中需要审核的数据
    buttonclick(auditDataele, '审核数据')
    auditselect(auditResultele,
                '审核通过',
                'xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120344-btn_AuditPass_label"]',
                'xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120344-btn_AuditNoPass_label"]')
    # 判断是否审核通过，新增审核不通过即为结束用例
    if notemsg == '资金分配uitest':
        if auditResultele == '审核通过':
            writeMethod('已审核', 'xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120344-searchForm"]/div/table/tbody/tr/td[4]/div/div[3]/input[1]')
        else:
            writeMethod('审核不通过', 'xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120344-searchForm"]/div/table/tbody/tr/td[4]/div/div[3]/input[1]')
        buttonclick('xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120344-btn_Search_label"]', '审核菜单查询按钮')
        buttonclick(auditDataele, '审核数据')
        buttonclick('xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120344-btn_AuditPass_label"]', '审核通过按钮')
        time.sleep(1)
        # check('手工调整项的审核状态为【审核通过】，不允许重复审核！', InputElement.get('保存成功校验文本'))
        buttonclick('xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120344-btn_AuditNoPass_label"]', '审核不通过按钮')
        time.sleep(1)
        # check('手工调整项的审核状态为【审核不通过】，不允许重复审核！', InputElement.get('保存成功校验文本'))
        buttonclick('xpath://*[@id="frame_navigation_athena_dijit_TemplatePanel_2120344-btn_Export_label"]', '审核菜单导出按钮')
        time.sleep(5)
    else:
        pass
    if flag ==1:
        buttonclick('xpath://*[@id="content_tablist"]/div[4]/div/div[2]/span[3]', '页面关闭')
        getMenu('资金还款', 'xpath://a[@title="资金还款"]')
        # 转出部门选择
        writeMethod(department, 'css:#frame_navigation_athena_dijit_TemplatePanel_2120239-searchDeptSelect')
        # 转出账号选择
        writeMethod(account, 'css:#frame_navigation_athena_dijit_TemplatePanel_2120239-searchAccountSelect')
    else:
        pass








if __name__ == "__main__":
    pass














